#!/usr/bin/env python
# coding: utf-8

# # 字体文件优化中文

# ## 背景：项目中需要使用中文字体文件otf，但是字体文件特别大，里面包含了中日韩以及其他无用字符，希望删除无用的，保留中文

# In[1]:


import os, re, requests, time, threading
from requests.auth import HTTPBasicAuth


# In[12]:


# 用python写个脚本，读取文件fontdata.txt，使用正则，提取其中的name值里的十六进制数，输出到文件中
file = open('fontdata.txt', 'r')
data = file.read()
file.close()
regular = re.compile('name="uni(.*?)"/>', re.S)
names = regular.findall(data)

with open('unifile.txt', 'w') as f:
    for name in names:
        f.write(name+'\n')


# In[3]:


# post一个网页
def post_one_page(txt):
    url = 'https://www.qqxiuzi.cn/bianma/zifujiShow.php'
    headers = {
        'Content-type': 'application/x-www-form-urlencoded',
        'Host': 'www.qqxiuzi.cn',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
        'Cookie': 'PHPSESSID=putgd9c7t2p53nnvc1idr4p1r4; Hm_lvt_899df2cdf7f5a83a719fb1bb96982b18=1565664453; Hm_lpvt_899df2cdf7f5a83a719fb1bb96982b18=1565672980'
    }
    auth = HTTPBasicAuth('ch-ch', 'QAZqaz009')
    proxies = {
#         "http": "http://ch-ch:QAZqaz009@dl-proxy.neusoft.com:8080/",
#         "https": "http://ch-ch:QAZqaz009@dl-proxy.neusoft.com:8080/",

        "https": "http://ch-ch:QAZqaz009@proxy.neusoft.com:8080/"
    }
    data = {
        'text': txt,
        'coding': 'Unicode',
        'ary': '16',
        'ary2': '16',
        'token': '3f4b050c6012245f9877386bb4f3cba8'
    }
    try:
        session = requests.Session()
        res = session.post(url, headers=headers, data=data, timeout=5, proxies=proxies, auth=auth, verify=False)
#         res.raise_for_status()
        res.encoding = 'utf-8'
        requests.packages.urllib3.disable_warnings()
        html = res.text
        regular = re.compile('GB2312.*?<b>(.*?)</b>', re.S)
        results = regular.search(html)
#         print (results.group(1))
        return results.group(1)
    except Exception as exc:
        print ('post :', exc)


# In[2]:


# 读取文件fontdata.txt，提取其中的name值
with open('unifile.txt', 'r') as f:
    data = f.read()


# In[3]:


def work(filename, datalist):
    print ('start ', filename, len(datalist))
#     for data in datalist:
#         result = post_one_page(data)
#         if result == '没有':
# #             print ('该编码不是中文常用字', data)
#             with open('result_'+str(filename)+'.txt', 'a+') as f:
#                 f.write(data+'\n')


# In[4]:


# 把GB2312转换为Unicode编码表.txt内容读取到程序里
with open('GB2312ToUnicode.txt', 'r') as f:
    datagb = f.read()


# In[26]:


# 收集不是中文常用字的编码到文件中result.txt
datalist = data.split('\n')
threads = []

# gblist = datagb.split('\n')
file = open('result.txt', 'a+')

for i in range(0, len(datalist)):
    line = ('0x'+ datalist[i])
    if datagb.find(line) >= 0:
#         print ('ok', line)
        pass
    else:
#         print ('ng', line)
        file.write(datalist[i]+'\n')
file.close()
print ('Done')


# In[3]:


# 读取result.txt，去修改SourceHanSansCN-Regular.ttx文件，把符合这个id的这几行文字都删除
with open('result.txt', 'r') as f:
    data = f.read()


# In[4]:


len(data.split('\n'))


# In[7]:


# 读取result.txt，去修改SourceHanSansCN-Regular.ttx文件，把符合这个id的这几行文字都删除
# CharStrings之间的跳过，节省时间
def delsavefile(file, lines):
    print (file, len(lines))
    filewrite = open('SourceHanSansCN-Regular_filewrite_'+str(file)+'.ttx', 'a+')
    
    charStrings_start = False
    for line in lines:  # 每一行数据
        exist = False
        if line.find('<CharStrings>') >= 0:
            charStrings_start = True
            continue
        if line.find('</CharStrings>') >= 0:
            charStrings_start = False
            continue
        if charStrings_start:
            continue
            
        for code in data.split('\n'):
            if code == '' or code.strip() == '':
                break
            if line.find(code.upper()) >= 0 or line.find(code.lower()) >= 0:
                if line.find('GlyphID') >= 0 or line.find('ClassDef') >= 0 or line.find('mtx name=') >= 0 or line.find('map code=') >= 0:
                    exist = True   # 含有无用id
                    break
            else:
                pass

        if exist:
            continue
        else:
            try:
                filewrite.write(line)
            except:
                print ('error: ', file, len(lines), line)
    filewrite.close()    


# In[8]:


def getttx(start, end):
    with open('SourceHanSansCN-Regular.ttx', 'r' , encoding='utf-8') as f: # 
        return (f.readlines()[start: end])


# In[9]:


# # 使用线程 删除无用字符
# threads = []
# for i in range(0, 1342211, 10000):
#     s = getttx(i, i+9999)
#     threadobj = threading.Thread(target=delsavefile, args=(i, s))
#     threads.append(threadobj)
#     threadobj.start()
# for t in threads:
#     t.join()
# print ('Done')


# In[10]:


delsavefile(0, getttx(0, 1342212))


# In[12]:


# # 合并文件
# file = open('SourceHanSansCN-Regular_r.ttx', 'w')
# for i in range(0, 1342211, 10000):
# #     print (i)
#     with open('SourceHanSansCN-Regular_filewrite_'+str(i)+'.ttx', 'r') as f:
#         onefile = f.read()
#         file.write(onefile)
# file.close()


# In[2]:


fileread = open('SourceHanSansCN-Regular.ttx', 'r', encoding='utf-8')
filewrite = open('SourceHanSansCN-Regular_filewrite_char.ttx', 'a+', encoding='utf-8')
lines = fileread.readlines()
# print (lines[18702-1: 18702+20])
flag = False
for line in lines:
    if line.find('CharStrings') >= 0:
        flag = True
    if line.find('/CharStrings') >= 0:
        flag = False
    if flag:
        filewrite.write(line)
fileread.close()
filewrite.close()


# In[5]:


# 删除 CharString 
def check(code):
    fileread = open('SourceHanSansCN-Regular_filewrite_char.ttx', 'r', encoding='utf-8')
    lines = fileread.readlines()
#     print (lines[:20])
    fileread.close()

    
    filewrite = open('SourceHanSansCN-Regular_filewrite_char.ttx', 'w', encoding='utf-8')
    mystate = False
    hasfind = False
    i = 0
    for line in lines:
    #     if line.find('')
        
#         for code in datas:
        i += 1
        if line.find('<CharString name') >= 0:
            mystate = True
        elif line.find('</CharString>') >= 0:
            mystate = False
            
            if hasfind:
                break
        if not mystate:
            filewrite.write(line)
            continue

        if line.find(code.upper()) >= 0 or line.find(code.lower()) >= 0:
            mystate = False
            hasfind = True
        else:
            pass

        if mystate:
            filewrite.write(line)
        
    for i in range(i, len(lines)):
        filewrite.write(lines[i])
    filewrite.close()


# In[6]:


datas = data.split('\n')
for code in datas:
    if code == '' or code.strip() == '':
        continue
    check(code)
print ('done')


# In[2]:


# 把结果文件分割成两部分，中间插入CharStrings数据
fileread = open('SourceHanSansCN-Regular_filewrite_0.ttx', 'r')
lines = fileread.readlines()
front = lines[:18702]
real = lines[18702:-1]
fileread.close()

# 输出文件
filewrite = open('SourceHanSansCN-Regular_filewrite_1.ttx', 'a+')
for fr in front:
    filewrite.write(fr)
    
num = 0
# for linewrite in filewrite.readlines():
# #     print (num, linewrite)
#     if linewrite.find('/Private') >= 0:
#         print (num, linewrite)
        
with open('SourceHanSansCN-Regular.ttx', 'r' , encoding='utf-8') as f: # 
    charStrings_start = False
    for line in f.readlines():
        if line.find('CharStrings') >= 0:
            charStrings_start = True
        if line.find('/CharStrings') >= 0:
            charStrings_start = False
            filewrite.write(line)
            break
        if charStrings_start:
#             print (line)
            filewrite.write(line)

# num += 1
    
for fr in real:
    filewrite.write(fr)
    
filewrite.close()
print ('Done')


# In[ ]:




